//
//  AppDelegate.m
//  YDYS
//
//  Created by zuqiu on 17/4/18.
//  Copyright © 2017年 zuqiu. All rights reserved.
//
#import "APIKey.h"
#import "AppDelegate.h"
#define APP_DownloadURL @"itms-services://?action=download-manifest&url=https://www.sunsung.cc:9443/ios/fdhsydwh2017.plist"

@interface AppDelegate ()<UIAlertViewDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    application.statusBarHidden = NO;
    application.statusBarStyle = UIStatusBarStyleLightContent;
    //  版本更新开关
    NSString  *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://211.138.191.87:9055/hswh_webapp/hswh_service/hswh.text"] encoding:NSUTF8StringEncoding error:nil];
//    NSString *string = @"1.2@no";
    NSString *serviceVersion = [string componentsSeparatedByString:@"@"].firstObject;
    NSString *isUpdate = [ string componentsSeparatedByString:@"@"].lastObject;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:serviceVersion forKey:@"serviceVersion"];
    [userDefaults synchronize];
    
    // 判断是否更新 传入yes更新
    if(![isUpdate isEqualToString:@"no"])//_roaldSearchText
    {
        [self updateApp:serviceVersion];
    }
    else
    {
//        [self updateApp:serviceVersion];

    }

    
    return YES;
}

- (void)updateApp:(NSString *)updateVersion
{
        //获取当前设备中应用的版本号
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        
        //判断两个版本是否相同 本地版本1.2 更新版本1.3
        if (![currentVersion isEqualToString:updateVersion]) {
            NSString *titleStr = [NSString stringWithFormat:@"检查更新：%@", @"黄山外呼"];
            NSString *messageStr = [NSString stringWithFormat:@"发现新版本（%@）,是否更新", updateVersion];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"立即升级" otherButtonTitles:nil, nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                [alert show];
            });
        } else {  //版本号和app store上的一致
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIHelper showTipsWithText:@"当前已是最新版本！" inView:nil];
            });
        }
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 
        if (buttonIndex == 0) { //点击”升级“按钮，就从打开app store上应用的详情页面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_DownloadURL]];
        }

}



@end
